package vcp.server.requesthandler;

import java.util.List;

import ocsf.server.ConnectionToClient;
import vcp.client.reports.EmptyReport;
import vcp.client.reports.QuarterReports;
import vcp.client.reports.ReportAbstract;
import vcp.client.reports.SubscriptionsReport;
import vcp.client.reports.WorkerRecordsReport;
import vcp.client.request.InformationRetrieveRequest;
import vcp.client.request.response.ResponseRequest;
import vcp.entity.Customer;
import vcp.entity.PeriodicReport;
import vcp.entity.Subscription;
import vcp.entity.Worker;
import vcp.enums.JobType;
import vcp.server.DAO.CustomerDAO;
import vcp.server.DAO.PeriodicReportDAO;
import vcp.server.DAO.SubscriptionDAO;
import vcp.server.DAO.WorkerDAO;
import vcp.server.interfaces.IServer;

public class InformationRetrieveRequestHandler extends RequestHandler <InformationRetrieveRequest>
{

	public InformationRetrieveRequestHandler ( IServer server )
	{
		super(server);
	}

	@Override
	public void execute ( InformationRetrieveRequest request, ConnectionToClient client )
	{
		ResponseRequest response = new ResponseRequest(request.getRequestID(), null);
		ReportAbstract report = new EmptyReport();

		switch ( request.getForm().getType() )
		{
			case QUARTER_REPORTS:
				report = this.retrieveQuarterReports();
				break;
			case SUBSCRIPTION:
				report = this.retrieveSubscriptions();
				break;
			case WORKER_RECORDS:
				report = this.retrieveWorkerRecords();
				break;
		}

		response.setData(report);
		this.sendResponse(response, client);
	}

	/**
	 * @return WorkerRecordsReport on success, otherwise EmptyReport
	 */
	private ReportAbstract retrieveWorkerRecords ()
	{
		// DAO's which be used for retrieval
		WorkerDAO workerDAO = new WorkerDAO();

		List <Worker> workers = workerDAO.findAllWorkers();

		// removes CEO from list
		this.filterCEOFromWorkerList(workers);
		WorkerRecordsReport report = new WorkerRecordsReport();
		for ( Worker worker : workers )
		{
			report.add(worker);
		}

		return report.hasData()
								? report
								: new EmptyReport();
	}

	/**
	 * @return SubscriptionsReport on success, otherwise EmptyReport
	 */
	private ReportAbstract retrieveSubscriptions ()
	{
		// DAO's which be used for retrieval
		SubscriptionDAO subscriptionDAO = new SubscriptionDAO();
		CustomerDAO customerDAO = new CustomerDAO();

		// generated reports
		SubscriptionsReport subscriptionsReport = new SubscriptionsReport();

		// search for all current subscriptions
		List <Subscription> subscriptions = subscriptionDAO.findAllSubscriptions();
		for ( Subscription subscription : subscriptions )
		{
			Customer customer = customerDAO.findByID(subscription.getCustomerID());
			subscriptionsReport.add(subscription, customer);
		}

		return subscriptionsReport.hasData()
											? subscriptionsReport
											: new EmptyReport();
	}

	/**
	 * @return QuarterReports on success, otherwise EmptyReport
	 */
	private ReportAbstract retrieveQuarterReports ()
	{
		// DAO's which be used for retrieval
		PeriodicReportDAO periodicReportDAO = new PeriodicReportDAO();

		// get all periodic reports
		List <PeriodicReport> periodicReports = periodicReportDAO.findAll(null);

		QuarterReports report = new QuarterReports();
		for ( PeriodicReport periodicReport : periodicReports )
		{
			report.add(periodicReport);
		}

		return report.hasData()
								? report
								: new EmptyReport();
	}

	/**
	 * filters CEO from given list
	 * 
	 * @param workers
	 */
	private void filterCEOFromWorkerList ( List <Worker> workers )
	{
		for ( int i = 0; i < workers.size(); i ++ )
		{
			if ( workers.get(i).getJobType() == JobType.CEO )
			{
				workers.remove(i);
				break;
			}
		}
	}
}
